import pandas as pd
import numpy as np

# 多层索引对象的索引与切片操作
# 1.series的操作
#   -- 对于series来说，直接中括号[]和使用.loc（）完全一样
df = pd.Series(
    np.random.randint(10, 100, size=6),
    index=[
        ['1班', '1班', '1班', '2班', '2班', '2班'],
        ['小明', '小红', '小黄', '小白', '小黑', '小紫']
    ]
)
print(df)
# 1.2显示索引
print(df['1班'])
print(df.loc['1班'])
print(df[['1班']])
print(df[['1班', '2班']])
print(df['1班']['小明'])
print(df.loc['1班']['小明'])
print(df.loc['1班', '小明'])
print(df['1班', '小明'])

# 1.3隐式索引
print(df[2])
print(df.iloc[2])
print(df.iloc[[2, 1]])

# 1.4切片
print("\n切片")
# 显示切片
print(df['1班':'2班'])
print(df.loc['1班':'2班'])
print(df.loc['1班'][:])

# 建议使用隐式索引
print(df[1:5])
print(df.iloc[1:5])












